<template>
  <el-card class="container-card">
    <el-table
      v-loading="loading"
      :data="skuList"
      border
      class="container-table"
    >
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="名称" prop="skuName"/> 
      <el-table-column label="描述" prop="skuDesc"/> 
      <el-table-column label="默认图片" prop="tmName" width="150">
        <template v-slot="{ row }">
          <img
            :src="row.skuDefaultImg"
            :alt="row.ImgName"
            style="width:100px;height:100px"
          />
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight" width="80">
      </el-table-column>
      <el-table-column label="价格" prop="price" width="80" /> 
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            :type="`${row.isSale ? 'info' : 'success'}`"
            :icon="`el-icon-${row.isSale ? 'bottom' : 'top'}`"
            size="mini"
            @click="
              ;`${
                row.isSale ? `${skuOnCancelId(row)}` : `${skuOnSaleById(row)}`
              }`
            "
            >{{ row.isSale ? '下架' : '上架' }}</el-button
          >
          <el-button type="warning" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            style="margin-left:10px"
            icon-color="red"
            :title="`确定删除${row.skuName}?`"
            @onConfirm="deleteSkuById(row.id)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next, jumper, total, sizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </el-card>
</template>

<script>
import {
  reqGetSkuList,
  reqDeleteSkuById,
  reqSkuOnSaleById,
  reqSkuCalcelById
} from '@/api/product/sku'
export default {
  name: 'Sku',
  data() {
    return {
      skuList: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList() {
      this.loading = true
      const res = await reqGetSkuList(this.currentPage, this.pageSize)
      this.skuList = res.records
      this.total = res.total
      this.loading = false
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getSkuList()
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.getSkuList()
    },
    async deleteSkuById(skuId) {
      await reqDeleteSkuById(skuId)
      this.$message.success('删除成功')
      this.getSkuList()
    },
    async skuOnSaleById(sku) {
      await reqSkuOnSaleById(sku.id)
      this.$message.success(`${sku.skuName}上架成功`)
      this.getSkuList()
    },
    async skuOnCancelId(sku) {
      await reqSkuCalcelById(sku.id)
      this.$message.success(`${sku.skuName}下架成功`)
      this.getSkuList()
    }
  }
}
</script>

<style></style>
